<?php
###############   COPYLEFT GPLv3 LICENSE   ###############
##
## JFX Version 0.2.9
## Website Management Software
## www.jfxcms.com
##
## Copyright 2009 GPLv3 - http://www.opensource.org/licenses/gpl-3.0.html
##
## Anthony Gallon
## oi_antz@hotmail.com
##
## Permission is hereby granted to any person having a copy of this software
## to freely use and modify as required so long as the copyright notices
## and branding remain intact.
##
## Full license details available at http://www.jfxcms.com/license
##
###############   COPYLEFT GPLv3 LICENSE   ###############
//
// this script can take several minutes to execute
set_time_limit(600);


$DB = JFX::registry('db');
$USER = JFX::registry('IUsers_User');
$CONFIG = JFX::registry('config');

// first, we uninstall previous version to start with a clean slate
$this->uninstall();

//$DB->debug(true);
//$DB->showErrors(true);
//$haltInstall = true;


$dirname = dirname(__FILE__);
$dirname = str_replace(DIRECTORY_SEPARATOR, '/', $dirname);

$error = false;












$tablesFile = 'tables.mysql.sql';
$tableDefinition = file_get_contents($dirname.'/'.$tablesFile);



// add the system prefix to the tables
$tableDefinition = str_replace('prefix_', $CONFIG->dbprefix, $tableDefinition);
/*
$sr = Antz::newObject('Antz_Db_SqlReader');
$sr->setQuery($tableDefinition);
// create our tables
while($q = $sr->nextQuery()){
    $DB->query($q);
};*/
$tableDefinition = explode(';', $tableDefinition);
foreach($tableDefinition as $k=>$v){
    if(trim($v)=='') continue;
    $DB->query($v);
};

// add the config settings
if(file_exists('bup_config.txt')){
    $DB->query("LOAD DATA INFILE '{$dirname}/bup_config.txt' INTO TABLE {$CONFIG->dbprefix}iusers_config");
}else{
    ob_start();
    include($dirname.'/config.sql');
    $config = ob_get_contents();
    ob_end_clean();
    $config = str_replace('prefix_', $CONFIG->dbprefix, $config);
    $config = explode(';', $config);
    foreach($config as $k=>$v){
        if(trim($v)=='') continue;
        $DB->query($v);
    };
};

/*
if(file_exists($dirname.'/bup_users.txt')){
    $DB->query("LOAD DATA INFILE '{$dirname}/bup_users.txt' INTO TABLE {$CONFIG->dbprefix}iusers");
};
if(file_exists($dirname.'/bup_user_groups.txt')){
    $DB->query("LOAD DATA INFILE '{$dirname}/bup_groups.txt' INTO TABLE {$CONFIG->dbprefix}iusers_groups");
};
if(file_exists($dirname.'/bup_users_to_groups.txt')){
    $DB->query("LOAD DATA INFILE '{$dirname}/bup_users_to_groups.txt' INTO TABLE {$CONFIG->dbprefix}iusers_to_groups");
};
if(file_exists($dirname.'/bup_fe_history.txt')){
    $DB->query("LOAD DATA INFILE '{$dirname}/bup_history.txt' INTO TABLE {$CONFIG->dbprefix}iusers_history");
};
if(file_exists($dirname.'/bup_countries.txt')){
    $DB->query("LOAD DATA INFILE '{$dirname}/bup_countries.txt' INTO TABLE {$CONFIG->dbprefix}iusers_countries");
}elseif(file_exists($dirname.'/countries.txt')){
    $DB->query("LOAD DATA INFILE '{$dirname}/countries.txt' INTO TABLE {$CONFIG->dbprefix}iusers_countries");
};
if(file_exists($dirname.'/bup_cities.txt')){
    $DB->query("LOAD DATA INFILE '{$dirname}/bup_cities.txt' INTO TABLE {$CONFIG->dbprefix}iusers_cities");
}else{
    $DB->query("LOAD DATA INFILE '{$dirname}/cities.txt' INTO TABLE {$CONFIG->dbprefix}iusers_cities");
};
if(file_exists($dirname.'/bup_pages_to_groups.txt')){
    // load page permission settings from the last installation
    $DB->query("LOAD DATA INFILE '{$dirname}/bup_pages_to_groups.txt' INTO TABLE {$CONFIG->dbprefix}iusers_pages_to_groups");
}else{
    // we have to make sure all pages get a permission setting, preventing the pages from being inaccessible
    $pages = $DB->fetchAll("SELECT id FROM {$CONFIG->dbprefix}pages");

    foreach($pages as $k=>$v){
        if($DB->countRows($CONFIG->dbprefix.'iusers_pages_to_groups', "page_id = '{$v['id']}'") == 0)
            $DB->insert($CONFIG->dbprefix.'iusers_pages_to_groups', array('page_id'=>$v['id'], 'group_key'=>'public'));
    };
};
*/

    // ending mysql setup

// create our default user groups
$DB->insert($CONFIG->dbprefix.'iusers_groups', array('keyname'=>'public', 'title'=>'Public'));
$DB->insert($CONFIG->dbprefix.'iusers_groups', array('keyname'=>'private', 'title'=>'Private'));
$DB->insert($CONFIG->dbprefix.'iusers_to_groups', array('group_key'=>'public', 'user_id'=>0));




// create our permissions in the system that will enable us to restrict access to the module
$this->createPermission('perm_config');
$this->createPermission('perm_users');
$this->createPermission('perm_groups');
$this->createPermission('perm_places');




// create default group to page associations for existing pages
$pages = $DB->fetchAll("SELECT id FROM {$CONFIG->dbprefix}pages");
foreach($pages as $k=>$v){
    $dbParams = array(
        'group_key' => 'public',
        'page_id' => $v['id']
    );
    $DB->insert($CONFIG->dbprefix.'iusers_pages_to_groups', $dbParams);
    $dbParams = array(
        'group_key' => 'private',
        'page_id' => $v['id']
    );
    $DB->insert($CONFIG->dbprefix.'iusers_pages_to_groups', $dbParams);
}









